import json

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from drf_yasg2 import openapi
from drf_yasg2.utils import swagger_auto_schema
from rest_framework.decorators import api_view
from rest_framework.parsers import JSONParser
from rest_framework.response import Response
from rest_framework import status
from rest_framework.views import APIView

from .models import *
from .serializers import UserPW_serializers, UserInfo_serializers

# Create your views here.
from .spider import land_sise_get_info


@api_view(['POST'])
def Login(request):
    if request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = UserPW_serializers(data=data)
        a = land_sise_get_info(url='http://class.seig.edu.cn:7001/sise/module/student_schedular/student_schedular.jsp',
                               user=data['StudentID'],
                               pw=data['Password'])
        if a:
            UserInfo.objects.create(StudentName=a[1], StudentID=a[0], StudentMajor=a[3], StudentYear=a[2])
            if serializer.is_valid():
                serializer.save()
                return JsonResponse(serializer.data, status=status.HTTP_200_OK)
        return JsonResponse(status=status.HTTP_400_BAD_REQUEST)


# class UserList(APIView):
#     """UserList cbv classdoc"""
#     # @swagger_auto_schema(operation_description="image GET description override",
#     #                      request_body=openapi.Schema(
#     #                          type=openapi.TYPE_OBJECT,
#     #                          required=['StudentID'],
#     #                          properties={
#     #                              'StudentID': openapi.Schema(type=openapi.TYPE_STRING)
#     #                          },
#     #                      ),
#     #                      )
#     def get(self, request):
#         queryset = UserInfo.objects.all()
#         serializer = UserInfo_serializers(queryset, many=True)
#         return Response(serializer.data)
